package com.aauedu.springboot.controller;

import com.aauedu.bean.Monster;
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class ParameterController {
    /**
     * /monster/{id}/{name}解读
     * 1./monster/{id}/{name}  构成完整请求路径
     * 2.{id} {name}就是占位变量
     * 3.@PathVariable("name")：这里name和{name}命名保持一致
     * 4.String name-这里自定义
     * 5.@PathVariable Map<String, String> map把所有传递的值传入map
     * 6.可以看下@PathVariable源码
     */

    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) {
        System.out.println("id--" + id);
        System.out.println("name--" + name);
        System.out.println("map--" + map);
        return "success";
    }
    // 获取请求头的信息

    /**
     * @RequestHeader("HOST"):获取http请求头的host信息，HOST不区分大小写
     * @RequestHeader Map<String, String> header:获取到http请求的所有信息
     * @param host
     * @param map
     * @param accept
     * @return
     */
    @GetMapping("/requestHeader")
    public String requestHeader(@RequestHeader("HOST") String host,
                                @RequestHeader Map<String, String> map,
                                @RequestHeader("accept") String accept) {
        System.out.println("host=" + host); //ip+port
        System.out.println("map=" + map);
        System.out.println("accept=" + accept);
        return "success";
    }

    /**
     * @RequestParam(value = "name", required = true)
     * name/value：用于指定参数名称；
     * required：默认为true，代表是否必传，当为true而参数缺失时抛出MissingServletRequestParameterException
     * 如果我们希望将所有的请求参数的值都获取到，可以通过
     * @RequestParam Map<String, String> map 获取到，注意多个键值对只能获取第一个，因为Map中的key是唯一的
     * @param name
     * @param hobbes
     * @return
     */
    @GetMapping("/requestParam")
    public String requestParam(@RequestParam(value = "name", required = true) String name,
                               @RequestParam("hobby")List<String> hobbes,
                               @RequestParam Map<String, String> map) {
        System.out.println("name-" + name);
        for (String hobby : hobbes) {
            System.out.println("hobby-" + hobby);
        }

        //遍历map
        /**
         * 方式一
         * for(Map.Entry<String, String> entry: map.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }*/
        /**
         * 方式二
        Iterator<String> iterator = map.keySet().iterator();
        while(iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key);
            System.out.println(map.get(key));
        }*/
//        System.out.println("map-" + map);
        return "success";
    }
    /**
     * 因为我的浏览器目前没有 cookie，所以就不测试， 大家知道怎么使用即可
     * 如果要测试，可以先写一个方法，在浏览器创建对应的 cookie
     * 说明 1. value = "cookie_key" 表示接收名字为 cookie_key 的 cookie
     * 2. 如果浏览器携带来对应的 cookie , 那么 后面的参数是 String ,则接收到的是对应对 value
     * 3. 后面的参数是 Cookie ,则接收到的是封装好的对应的 cookie
     */
    @GetMapping("/cookie")
    public String cookieValue(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                              @CookieValue(value = "username", required = false) Cookie cookie,
                              HttpServletRequest request) {
        System.out.println("cookie_value-" + cookie_value);
        System.out.println("username-" + cookie.getName() + "-" + cookie.getValue());
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1.getName() + "->" + cookie1.getValue());
        }
        return "success";
    }

    @PostMapping("/save")
    public String requestBody(@RequestBody(required = false) String context) {
        System.out.println("context-" + context);
        return "success";
    }

    @PostMapping("/saveMonster")
    public String saveMonster(Monster monster) {
        System.out.println("monster=" + monster);
        return "success";
    }
}
